import { inject, type App, type InjectionKey } from 'vue'

const ChartResizeSymbol: InjectionKey<ChartState> = Symbol()
const chartResize = {
  install(app: App<any>) {
    const chart = new ChartState()
    app.provide(ChartResizeSymbol, chart)
  }
}

class ChartState {
  #chartsList: any[] = []

  constructor() {}

  get list() {
    return this.#chartsList
  }

  add(chart: any) {
    this.#chartsList.push(chart)
  }

  clear() {
    this.#chartsList = []
  }
}

export const useChartResize = () => {
  const chartMachine = inject(ChartResizeSymbol)

  if (!chartMachine) {
    throw Error('useChartResize() without provider')
  }

  return chartMachine
}

export default chartResize
